/*
 * ChatBuffer.java
 *
 * Created on April 21, 2007, 9:22 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package d20chat;

import java.util.Vector;

/**
 * Maintains buffer that handles chat text
 * Autoresizing buffer. Is given pieces of text which it adds to the buffer. When
 * requested it will give a portion of the buffer in a text string.
 * @author Scott
 */
public class ChatBuffer 
{
    private Vector<String> buffer;
    private int buffersize;
    
    /**
     * Creates a new instance of ChatBuffer.
     * Pass in the initial display length of the buffer.
     * @param size defines how many lines to return when using gettext
     */
    public ChatBuffer(int size) 
    {
        buffersize = size;
        buffer = new Vector<String>();
    }
    
    /**
     * Adds a single line to the buffer.
     * @param linein Line of text to be inserted at the end of the buffer.
     */
    public void addline(String linein)
    {
        buffer.add(linein);
    }
    
    /**
     * Flushes the buffer clearing it completely.
     * This is not able to be undone. It will clear the buffer and resize the buffer
     * to the default size.
     */
    public void clearbuffer()
    {
        buffer.clear();
        buffer.trimToSize();
    }
    
    /**
     * Returns the most recently inserted portion of the buffer.
     * Length of the text returned is specified using setsize.
     * @return String containing most recently inserted portion of the buffer.
     */
    public String gettext()
    {
        int start = buffer.size() - buffersize;
        if(start < 0)
        {
            start = 0;
        }
        String builder = "";
        for(int i = start; i < buffer.size(); i++)
        {
            builder = builder + buffer.get(i);
        }
        return builder;
        //List temp = buffer.subList(start, buffer.size());
        //return buffer.toString();
        
    }
    
    /**
     * Sets the length of the string returned when gettext is used.
     * This is just the display length, not the actual length of the buffer.
     * @param size Amount of lines to be returned when using gettext.
     */
    public void setsize(int size)
    {
        buffersize = size;
    }
    
    /**
     * Fetches the display size of the buffer.
     * This is the amount of lines returned when using gettext, not the actual
     * length of the buffer.
     * @return The display size of the buffer.
     */
    public int getsize()
    {
        
        return buffersize;
    }
    
    /**
     * Fetches the entire buffer.
     * Useful for when needing to save the entire buffer to a file such as saving
     * the transcript.
     * @return The entire buffer in a single string.
     */
    public String getall()
    {
        String builder = "";
        for(int i = 0; i < buffer.size(); i++)
        {
            builder = builder + buffer.get(i);
        }
        return builder;
    }
    
}
